﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Web_Blog.Controler
{
    public class Friend
    {
        //Load danh sách bạn bè
        public static string Load(int uID)
        {
            int count = -1;
            string retval = "<tr>";

            ConnectDB.ConnectDB DB = new ConnectDB.ConnectDB();
            SqlConnection conn = DB.LayKetNoi();
            conn.Open();

            SqlCommand command = new SqlCommand("procLayFriend", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@uID", uID));
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                //retval += reader["IDlist"].ToString();      //Load tên list friend (ví dụ như THCS, THPT, Unknow)
                //retval += ": <br/>";                          //Phân cách với friend

                string[] likearr = reader["IDfriend"].ToString().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < likearr.Length; i++)
                {
                    
                    ConnectDB.ConnectDB DB2 = new ConnectDB.ConnectDB();
                    SqlConnection conn3 = DB.LayKetNoi();
                    conn3.Open();

                    SqlCommand command2 = new SqlCommand("procLayUser", conn3);
                    command2.CommandType = CommandType.StoredProcedure;
                    command2.Parameters.Add(new SqlParameter("@uID", likearr[i]));
                    SqlDataReader reader2 = null;
                    reader2 = command2.ExecuteReader();
                    if (reader2.Read())
                    {
                        count++;                                        //Biến đếm để biết xuống dòng
                        if ((count % 4) == 0) retval += "</tr><tr>";
                        retval +="<td class=\"style6\" height=\"140\">  <a href=\"";
                        retval +="TrangChu.aspx?u="+reader2["uID"].ToString();   //Đường dẫn link
                        retval +="\"> <img src=\"";
                        retval += Controler.Other.ParseAvatar(reader2["avatar"].ToString());
                        retval +="\" style=\"width:125px;height:125px\" /><br />";
                        retval +=reader2["name"].ToString();
                        retval +="</a> </td>";
                    }
                    reader2.Close();

                    
                    conn3.Close();
                }
            }

            conn.Close();
            retval += "</tr>";                                  //Đóng dòng cuối cùng
            retval = retval.Replace("<tr></tr>", "");
            return retval;
        }

        //Tạo danh sách friend
        public static int CreateList(Object.CreateFriendListObject ls)
        {
            ConnectDB.ConnectDB DB = new ConnectDB.ConnectDB();
            SqlConnection conn = DB.LayKetNoi();
            conn.Open();

            SqlCommand command = new SqlCommand("procTaoListFriend", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@uID", ls.UID));
            command.Parameters.Add(new SqlParameter("@list", ls.List));
            
            int retval= command.ExecuteNonQuery();

            conn.Close();
            return retval;
        }

        //Kết bạn
        public static int MakeFriend(Object.MakeFriendObject mf)
        {
            bool added = false;

            ConnectDB.ConnectDB DB = new ConnectDB.ConnectDB();
            SqlConnection conn = DB.LayKetNoi();
            conn.Open();

            SqlCommand command = new SqlCommand("procLayFriend", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@uID", mf.MyID));
            SqlDataReader reader = command.ExecuteReader();

            if (mf.List == "") mf.List = "Friends";

            while (reader.Read()&&added==false)                             //Đọc từng list
            {
                if (reader["IDlist"].ToString().Equals(mf.List))            //Nếu đúng danh sách thì mới thêm
                {
                    string danhsachban = reader["IDfriend"].ToString();
                    reader.Close();
                    danhsachban += ";" + mf.FriendID + ";";
                    SqlCommand command2 = new SqlCommand("procKetBan", conn);
                    command2.CommandType = CommandType.StoredProcedure;
                    command2.Parameters.Add(new SqlParameter("@uID", mf.MyID));
                    command2.Parameters.Add(new SqlParameter("@list", mf.List));
                    command2.Parameters.Add(new SqlParameter("@friends", danhsachban));

                    if (command2.ExecuteNonQuery() == 1) added = true;

                    break;
                }
            }

            if (added == false)         //Add cho lần đầu tiên nếu ko điền list, tự động tạo list friend và add vào, hoặc là điền tên sai, tự tạo list mới
            {
                reader.Close();
                Object.CreateFriendListObject ls = new Object.CreateFriendListObject();
                ls.UID=mf.MyID;
                ls.List=mf.List;
                CreateList(ls);

                string danhsachban = ";" + mf.FriendID + ";";
                SqlCommand command2 = new SqlCommand("procKetBan", conn);
                command2.CommandType = CommandType.StoredProcedure;
                command2.Parameters.Add(new SqlParameter("@uID", mf.MyID));
                command2.Parameters.Add(new SqlParameter("@list", mf.List));
                command2.Parameters.Add(new SqlParameter("@friends", danhsachban));

                if (command2.ExecuteNonQuery() == 1) added = true;
            }
            conn.Close();
            if (added)
            {
                //Thêm thông báo
                Object.NotifyObject n = new Object.NotifyObject();
                n.Srcuid = mf.MyID;
                n.Desuid = mf.FriendID;
                n.Type = 3;
                Controler.Notification.AddNotification(n);

                return 1;
            }
            return 0;
        }

        //Kiểm tra xem đã là bạn chưa
        public static bool IsFriend(int myUID, int friendUID)
        {
            string temp=";"+friendUID+";";

            ConnectDB.ConnectDB DB = new ConnectDB.ConnectDB();
            SqlConnection conn = DB.LayKetNoi();
            conn.Open();

            SqlCommand command = new SqlCommand("procLayFriend", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@uID", myUID));
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                if (reader["IDfriend"].ToString().Contains(temp))
                    return true;
            }

            conn.Close();

            return false;
        }

        //Không kết bạn nữa
        public static void MakeUnFriend(Object.MakeFriendObject mf)
        {
            ConnectDB.ConnectDB DB = new ConnectDB.ConnectDB();
            SqlConnection conn = DB.LayKetNoi();
            conn.Open();

            SqlCommand command = new SqlCommand("procLayFriend", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@uID", mf.MyID));
            SqlDataReader reader = command.ExecuteReader();

            if (mf.List == "") mf.List = "Friends";

            while (reader.Read())                             //Đọc từng list
            {
                if (reader["IDlist"].ToString().Equals(mf.List))            //Nếu đúng danh sách thì mới thêm
                {
                    string danhsachban = reader["IDfriend"].ToString();
                    reader.Close();
                    string temp=";" + mf.FriendID + ";";
                    danhsachban = danhsachban.Replace(temp, "");
                    SqlCommand command2 = new SqlCommand("procKetBan", conn);
                    command2.CommandType = CommandType.StoredProcedure;
                    command2.Parameters.Add(new SqlParameter("@uID", mf.MyID));
                    command2.Parameters.Add(new SqlParameter("@list", mf.List));
                    command2.Parameters.Add(new SqlParameter("@friends", danhsachban));

                    command2.ExecuteNonQuery();

                    return;
                }
            }
        }
    }
}